<?php

/**
 * my_account actions.
 *
 * @package    academia_de_escritores
 * @subpackage my_account
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class my_accountActions extends sfActions
{

  public function executeShow(sfWebRequest $request)
  {
    if($this->getUser()->isAuthenticated()){
      $this->user = $this->getUser()->getGuardUser()->getUser();
      $this->forward404Unless($this->user);
    }  else {
      $this->redirect('@home');
    }
  }

  public function executeEdit(sfWebRequest $request)
  {
    if($this->getUser()->isAuthenticated()){
      $this->forward404Unless($user = $this->getUser()->getGuardUser()->getUser());
      $this->form = new UserAccountEditForm($user);
      $this->form->setDefault('email', $user->getSfGuardUser()->getEmailAddress());
    }  else {
      $this->redirect('@home');
    }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    if($this->getUser()->isAuthenticated()){
      $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
      $this->forward404Unless($user = $this->getUser()->getGuardUser()->getUser());
      $this->form = new UserAccountEditForm($user);
      $this->processForm($request, $this->form);
      $this->setTemplate('edit');
    }  else {
      $this->redirect('@home');
    }
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $user = $form->save();
      $this->getUser()->setFlash('mensaje', 'sus datos se han guardado exitosamente');
      $this->redirect('my_account');
    }
  }
}
